home *** CD-ROM | disk | FTP | other *** search
- ///--------------------------------------------------------------------------------------
- // Tiling Demo.c
- //
- // By Vern Jensen, August 1995
- ///--------------------------------------------------------------------------------------
-
-
- #include <SWIncludes.h>
- #include <SWGameUtils.h>
- #include <SWDitherDown.h>
- #include <SWFPSReport.h>
-
- #include "SWApplication.h"
- #include "Tiling Demo.h"
-
-
- #define kFullScreenWindow true // Makes the window fill the screen
- #define kInterlacedMode false // Turns Interlaced mode on/off
- #define kSyncToVBL false // Syncs animation to VBL
- #define kWorldRectInset 0 // Makes SpriteWorld smaller than window
- #define kMaxFPS 30 // Set to 0 for unrestricted speed
-
- #define kNumDiamonds 10 // Number of diamonds
- #define kMaxDiamondMoveDelta 5 // Max speed for the diamonds
-
- #define kTileWidth 40 // Don't change these unless you
- #define kTileHeight 40 // change the resource as well.
-
-
- enum tileIDs // Meaningful values for the tileIDs
- {
- kWallTile,
- kBoardTile,
- kMaxNumTiles
- };
-
-
-
- /***********/
- /* Globals */
- /***********/
-
- SpriteWorldPtr gSpriteWorldP;
-
- SpriteLayerPtr gTopDiamondSpriteLayerP;
- SpriteLayerPtr gBottomDiamondSpriteLayerP;
-
- TileMapStructPtr gTileMapStructP;
- SpritePtr gBallSpriteP;
- WindowPtr gWindowP;
-
- DrawProcPtr gSpriteDrawProc;
- short gNumTileMapRows, gNumTileMapCols;
-
-
- ///--------------------------------------------------------------------------------------
- // Main
- ///--------------------------------------------------------------------------------------
-
- void main( void )
- {
- Initialize(kNumberOfMoreMastersCalls);
-
- if (SWHasSystem7())
- {
- SetCursor(*GetCursor(watchCursor));
-
- CreateSpriteWorld();
- SetUpTiling();
- CreateSprites();
-
- SetCursor(&qd.arrow);
- HideCursor();
-
- SetUpAnimation();
- RunAnimation();
- ShutDown();
- }
- else
- {
- CantRunOnThisMachine();
- }
- }
-
-
- ///--------------------------------------------------------------------------------------
- // CreateSpriteWorld
- ///--------------------------------------------------------------------------------------
-
- void CreateSpriteWorld( void )
- {
- Rect offscreenRect, worldRect, windRect;
- RgnHandle mBarUpdateRgn;
- OSErr err;
-
- gWindowP = GetNewCWindow(kWindowResID, NULL, (WindowPtr)-1L);
-
- if (gWindowP != NULL)
- {
- if (kFullScreenWindow == true)
- {
- SizeWindow(gWindowP, qd.screenBits.bounds.right,
- qd.screenBits.bounds.bottom, false);
- MoveWindow(gWindowP, 0, 0, false);
- }
- else
- {
- // Center window in screen
- windRect = gWindowP->portRect;
- CenterRect(&windRect, &qd.screenBits.bounds);
- MoveWindow(gWindowP, windRect.left, windRect.top, false);
- }
-
- ShowWindow(gWindowP);
- SetPort(gWindowP);
- mBarUpdateRgn = SWHideMenuBar(gWindowP); // Must be done *after* showing window!
- EraseRgn(mBarUpdateRgn);
-
- if (kInterlacedMode)
- PaintRect(&gWindowP->portRect); // Blacken window for Interlaced mode
- }
- else
- CantFindResource();
-
-
- err = SWEnterSpriteWorld();
- FatalError(err);
-
-
- worldRect = gWindowP->portRect;
- InsetRect(&worldRect, kWorldRectInset, kWorldRectInset);
-
-
- // Set size of offscreen area
- offscreenRect = worldRect;
- OffsetRect(&offscreenRect, -offscreenRect.left, -offscreenRect.top);
-
-
- // Make offscreen area evenly divisible by tile width & height
- if ( (offscreenRect.right/kTileWidth)*kTileWidth != offscreenRect.right)
- offscreenRect.right = (offscreenRect.right/kTileWidth)*kTileWidth + kTileWidth;
-
- if ( (offscreenRect.bottom/kTileHeight)*kTileHeight != offscreenRect.bottom)
- offscreenRect.bottom = (offscreenRect.bottom/kTileHeight)*kTileHeight + kTileHeight;
-
-
- gNumTileMapRows = offscreenRect.bottom / kTileHeight;
- gNumTileMapCols = offscreenRect.right / kTileWidth;
-
-
- err = SWCreateSpriteWorldFromWindow(&gSpriteWorldP, (CWindowPtr)gWindowP,
- &worldRect, &offscreenRect, 0);
- FatalError(err);
-
-
- if (gSpriteWorldP->pixelDepth == 8) // 256 colors
- {
- gSpriteDrawProc = CompiledSprite8BitDrawProc;
- }
- else if ( !(SW_PPC && gSpriteWorldP->pixelDepth < 8) ) // not 256 colors
- {
- if (kInterlacedMode)
- gSpriteDrawProc = BPAllBitInterlacedMaskDrawProc;
- else
- gSpriteDrawProc = BlitPixieAllBitMaskDrawProc;
- }
- else
- {
- gSpriteDrawProc = SWStdSpriteDrawProc;
- }
- }
-
-
- ///--------------------------------------------------------------------------------------
- // SetUpTiling
- ///--------------------------------------------------------------------------------------
-
- void SetUpTiling( void )
- {
- short row, col;
- OSErr err;
-
-
- // Must be done before we can load the tiles.
- err = SWInitTiling(gSpriteWorldP, kTileHeight, kTileWidth, kMaxNumTiles);
- FatalError(err);
-
- err = SWCreateTileMap(&gTileMapStructP, gNumTileMapRows, gNumTileMapCols);
- FatalError(err);
-
- SWInstallTileMap(gSpriteWorldP, gTileMapStructP, 0);
-
-
- // For demonstration purposes, we've decided to load the tiles from CICN
- // resources here, and load them from PICT resources in the Scrolling Demo.
-
- // Load the wall tile
- err = SWLoadTileFromCicnResource(
- gSpriteWorldP,
- kWallTile, // tileID
- 200, // CICN resource ID
- kFatMask); // maskType
- FatalError(err);
-
- // Load the board ("wire") tile
- err = SWLoadTileFromCicnResource(
- gSpriteWorldP,
- kBoardTile, // tileID
- 201, // CICN resource ID
- kFatMask); // maskType
- FatalError(err);
-
-
- // Initialize the values in the tileMap
- for (row = 0; row < gNumTileMapRows; row++)
- {
- for (col = 0; col < gNumTileMapCols; col++)
- {
- if (row == 0 || col == 0 || row == gNumTileMapRows-1 || col == gNumTileMapCols-1)
- gTileMapStructP->tileMap[row][col] = kWallTile;
- else
- gTileMapStructP->tileMap[row][col] = kBoardTile;
- }
- }
- }
-
-
- ///--------------------------------------------------------------------------------------
- // CreateSprites
- ///--------------------------------------------------------------------------------------
-
- void CreateSprites( void )
- {
- SpritePtr diamondSpriteP;
- SpritePtr tempSpriteP;
- short spriteNum;
- Point moveDelta, mousePoint;
- Rect moveBounds;
- OSErr err;
-
-
- // Create the sprite layers
- SWCreateSpriteLayer(&gTopDiamondSpriteLayerP);
- SWCreateSpriteLayer(&gBottomDiamondSpriteLayerP);
- FatalError(SWStickyError());
-
-
- moveBounds = gSpriteWorldP->backRect;
- InsetRect(&moveBounds, 40, 40);
-
- // create and set up the diamond sprites
- for (spriteNum = 0; spriteNum < kNumDiamonds; spriteNum++)
- {
- if (spriteNum == 0)
- {
- err = SWCreateSpriteFromPictResource(gSpriteWorldP,
- &diamondSpriteP,
- NULL, // pointer to memory for sprite
- 128, // picture resource id
- 128, // mask resource id (we use self-masking)
- 1, // max frames
- kFatMask); // mask type
- FatalError(err);
-
- tempSpriteP = diamondSpriteP;
-
- if (gSpriteWorldP->pixelDepth == 8)
- SWCompileSprite(diamondSpriteP);
- }
- else
- {
- err = SWCloneSprite(diamondSpriteP, &tempSpriteP, NULL);
- FatalError(err);
- }
-
-
- SWSetSpriteMoveBounds(tempSpriteP, &moveBounds);
- SWSetSpriteMoveProc(tempSpriteP, DiamondSpriteMoveProc);
-
- SWSetSpriteLocation(tempSpriteP,
- GetRandom(40, gSpriteWorldP->backRect.right-80),
- GetRandom(40, gSpriteWorldP->backRect.bottom-80));
-
- do
- {
- moveDelta.h = GetRandom(-kMaxDiamondMoveDelta, kMaxDiamondMoveDelta);
- moveDelta.v = GetRandom(-kMaxDiamondMoveDelta, kMaxDiamondMoveDelta);
- } while (!moveDelta.v || !moveDelta.h);
-
- SWSetSpriteMoveDelta(tempSpriteP, moveDelta.h, moveDelta.v);
- SWSetSpriteDrawProc(tempSpriteP, gSpriteDrawProc);
- err = SWAddSprite(gTopDiamondSpriteLayerP, tempSpriteP);
- FatalError(err);
- }
-
-
- // Create the ball sprite
- err = SWCreateSpriteFromCicnResource(gSpriteWorldP, &gBallSpriteP, NULL,
- 128, 1, kFatMask);
- FatalError(err);
-
- if (gSpriteWorldP->pixelDepth == 8)
- SWCompileSprite(gBallSpriteP);
-
-
- // Set up the ball sprite
- err = SWAddSprite(gTopDiamondSpriteLayerP, gBallSpriteP);
- FatalError(err);
- SWSetSpriteMoveProc(gBallSpriteP, BallSpriteMoveProc);
- SWSetSpriteDrawProc(gBallSpriteP, gSpriteDrawProc);
- SWSetFrameHotSpot(gBallSpriteP->curFrameP, 20, 20);
- GetMouse(&mousePoint);
- SWSetSpriteLocation(gBallSpriteP, mousePoint.h, mousePoint.v);
-
- SWAddSpriteLayer(gSpriteWorldP, gBottomDiamondSpriteLayerP); // Add bottom first, so it is drawn first
- SWAddSpriteLayer(gSpriteWorldP, gTopDiamondSpriteLayerP); // Add top last, so it is drawn last
-
- SWLockSpriteWorld(gSpriteWorldP);
-
-
- // Test out the dithering routines in DitherDown.c.
- // See DitherDown.c for more info.
- if (gSpriteWorldP->pixelDepth < 8)
- {
- // Dither the diamond sprite
- DitherDownPict(128, diamondSpriteP->frameArray[0]->framePort);
- LowerMaskDepth(128, diamondSpriteP->frameArray[0]->maskPort);
-
- // Dither the wall tile
- DitherDownCicn(200, gSpriteWorldP->tileFrameArray[kWallTile]->framePort);
- }
- }
-
-
- ///--------------------------------------------------------------------------------------
- // SetUpAnimation
- ///--------------------------------------------------------------------------------------
-
- void SetUpAnimation( void )
- {
- SWSetSpriteWorldMaxFPS(gSpriteWorldP, kMaxFPS);
- SWSyncSpriteWorldToVBL(gSpriteWorldP, kSyncToVBL);
- SWSetCleanUpSpriteWorld(gSpriteWorldP);
-
- SWSetSpriteLayerUnderTileLayer(gBottomDiamondSpriteLayerP, 0);
-
-
- if (gSpriteWorldP->pixelDepth == 8) // 256 colors
- {
- if (kInterlacedMode)
- {
- SWSetSpriteWorldScreenDrawProc(gSpriteWorldP, BP8BitInterlacedRectDrawProc);
- SWSetSpriteWorldOffscreenDrawProc(gSpriteWorldP, BP8BitInterlacedRectDrawProc);
- SWSetPartialMaskDrawProc(gSpriteWorldP, BP8BitInterlacedPartialMaskDrawProc);
- }
- else
- {
- SWSetSpriteWorldOffscreenDrawProc(gSpriteWorldP, BlitPixie8BitRectDrawProc);
- SWSetSpriteWorldScreenDrawProc(gSpriteWorldP, BlitPixie8BitRectDrawProc);
- SWSetPartialMaskDrawProc(gSpriteWorldP, BlitPixie8BitPartialMaskDrawProc);
- }
- }
- else if ( !(SW_PPC && gSpriteWorldP->pixelDepth < 8) ) // not 256 colors
- {
- if (kInterlacedMode)
- {
- SWSetSpriteWorldScreenDrawProc(gSpriteWorldP, BPAllBitInterlacedRectDrawProc);
- SWSetSpriteWorldOffscreenDrawProc(gSpriteWorldP, BPAllBitInterlacedRectDrawProc);
- SWSetPartialMaskDrawProc(gSpriteWorldP, BPAllBitInterlacedPartialMaskDrawProc);
- }
- else
- {
- SWSetSpriteWorldOffscreenDrawProc(gSpriteWorldP, BlitPixieAllBitRectDrawProc);
- SWSetSpriteWorldScreenDrawProc(gSpriteWorldP, BlitPixieAllBitRectDrawProc);
- SWSetPartialMaskDrawProc(gSpriteWorldP, BlitPixieAllBitPartialMaskDrawProc);
- }
- }
-
-
- // Make sure CopyBits, if used, doesn't try to colorize things
- SWSetPortToWindow(gSpriteWorldP);
- ForeColor(blackColor);
- BackColor(whiteColor);
-
-
- SWDrawTilesInBackground(gSpriteWorldP);
- SWUpdateSpriteWorld(gSpriteWorldP, true);
- }
-
-
- ///--------------------------------------------------------------------------------------
- // RunAnimation
- ///--------------------------------------------------------------------------------------
-
-
- void RunAnimation( void )
- {
- unsigned long frames;
-
- frames = 0;
- StartTimer();
-
- FatalError( SWStickyError() ); // Make sure no errors got past us during setup
-
- while (!Button())
- {
- SWProcessSpriteWorld(gSpriteWorldP);
- FatalError( SWStickyError() ); // Make sure no errors occurred during a MoveProc, etc.
- SWAnimateSpriteWorld(gSpriteWorldP);
-
- if (gSpriteWorldP->frameHasOccurred)
- frames++;
- }
-
- SWShowMenuBar(gWindowP);
- ShowResults(frames);
- }
-
-
- ///--------------------------------------------------------------------------------------
- // ShutDown (clean up and dispose of the SpriteWorld)
- ///--------------------------------------------------------------------------------------
-
-
- void ShutDown( void )
- {
- SWUnlockSpriteWorld(gSpriteWorldP);
- SWDisposeSpriteWorld(&gSpriteWorldP);
- SWExitSpriteWorld();
-
- FlushEvents(everyEvent, 0);
- ShowCursor();
- }
-
-
- ///--------------------------------------------------------------------------------------
- // BallSpriteMoveProc - makes the ball follow the mouse
- ///--------------------------------------------------------------------------------------
-
- SW_FUNC void BallSpriteMoveProc(SpritePtr srcSpriteP)
- {
- Point mousePoint;
- SpritePtr tempSpriteP;
- EventRecord event;
- short theChar;
-
-
- // Move the ball sprite
- GetMouse(&mousePoint);
-
- // Subtract any offset from worldRect to window if a worldRect is used
- mousePoint.h -= gSpriteWorldP->windRect.left;
- mousePoint.v -= gSpriteWorldP->windRect.top;
-
- // This sprite uses a hotSpot of 20,20, so all we have to do is move its
- // hotPoint to the current mouse location, and it will appear centered properly
- SWMoveSprite(srcSpriteP, mousePoint.h, mousePoint.v);
-
-
- // Change ball sprite layers if the spacebar was hit
- while ( GetOSEvent(keyDownMask, &event) )
- {
- theChar = (event.message & charCodeMask);
-
- if (theChar == ' ')
- {
- if (srcSpriteP->parentSpriteLayerP == gBottomDiamondSpriteLayerP)
- {
- SWRemoveSprite(srcSpriteP);
- SWAddSprite(gTopDiamondSpriteLayerP, srcSpriteP);
- }
- else
- {
- SWRemoveSprite(srcSpriteP);
- SWAddSprite(gBottomDiamondSpriteLayerP, srcSpriteP);
- }
- }
- }
-
-
- // Check for collision with diamonds in same layer as ball //
-
- tempSpriteP = srcSpriteP->parentSpriteLayerP->headSpriteP;
-
- while (tempSpriteP != NULL)
- {
- // are the sprite’s rectangles overlapping?
- if (tempSpriteP != gBallSpriteP &&
- (tempSpriteP->destFrameRect.top < gBallSpriteP->destFrameRect.bottom) &&
- (tempSpriteP->destFrameRect.bottom > gBallSpriteP->destFrameRect.top) &&
- (tempSpriteP->destFrameRect.left < gBallSpriteP->destFrameRect.right) &&
- (tempSpriteP->destFrameRect.right > gBallSpriteP->destFrameRect.left))
- {
- // Check to see if the masks overlap
- if ( SWRegionCollision(tempSpriteP, gBallSpriteP))
- {
- // Switch diamond position above/under tiles
- if (tempSpriteP->parentSpriteLayerP == gBottomDiamondSpriteLayerP)
- {
- SWRemoveSprite(tempSpriteP);
- SWAddSprite(gTopDiamondSpriteLayerP, tempSpriteP);
- }
- else
- {
- SWRemoveSprite(tempSpriteP);
- SWAddSprite(gBottomDiamondSpriteLayerP, tempSpriteP);
- }
- }
- }
-
- tempSpriteP = tempSpriteP->nextSpriteP;
- }
- }
-
-
- ///--------------------------------------------------------------------------------------
- // DiamondSpriteMoveProc
- ///--------------------------------------------------------------------------------------
-
-
- SW_FUNC void DiamondSpriteMoveProc(SpritePtr ballSpriteP)
- {
- SWOffsetSprite(ballSpriteP, ballSpriteP->horizMoveDelta, ballSpriteP->vertMoveDelta);
- (void)SWBounceSprite(ballSpriteP);
- }
-
-